import React from 'react'
import './download.css'
import SquareIcon from '../../res/images/web/download/形状结合@2x.png'
import shijeibo from "../../res/images/web/shared/shijiebo.png"
// import logoUrl from '../../res/images/web/download/logo@2x.png'
import logoUrl from '../../res/images/web/shared/LOGO-YOUTOBALL@1x.png'
import CustomButton from "../../shared-components/CustomButton/CustomBotton";
import {getDownloadUrlReq} from "../../apis/goApis";
import {app_name} from "../../utils/constants";
const u = navigator.userAgent
export class Browser {

    static version = {
        trident: u.indexOf('Trident') > -1, //IE内核
        presto: u.indexOf('Presto') > -1, //opera内核
        webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
        gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') === -1, //火狐内核
        mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
        ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
        android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
        iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
        iPad: u.indexOf('iPad') > -1, //是否iPad
        webApp: u.indexOf('Safari') === -1, //是否web应该程序，没有头部与底部
        weixin: u.toLowerCase().indexOf('micromessenger') > -1,
    }

    static language = (navigator.browserLanguage || navigator.language).toLowerCase()

}

class Download extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            platform: '',
            ios: '',
            android: '',
            pc: 'https://www.youtoball.com',
        }
    }

    componentDidMount() {
        this.getUrls()
        this.setPlatform()
    }

    getUrls = async () => {
        const resp = await getDownloadUrlReq()
        const data = resp.data.ok
        const {
            ios,
            aod,
        } = data

        this.setState({
            ios,
            android: aod,
        })
    }

    setPlatform = () => {

        const mobile = Browser.version.mobile
        if (mobile) {
            const weixin = Browser.version.weixin
            const ios = Browser.version.ios
            const android = Browser.version.android
            if (weixin) {
                this.setState({
                    platform: 'weixin'
                })
            } else {
                if (ios) {
                    this.setState({
                        platform: 'ios',
                    })
                } else if (android) {
                    this.setState({
                        platform: 'android',
                    })
                }
            }

        } else {
            this.setState({
                platform: 'pc'
            })
        }

    }

    toDownload = () => {
        const platform = this.state.platform
        const m = this.state
        const end = m[platform]
        window.location.href = end
    }

    render() {
        const platform = this.state.platform
        const wx = platform === 'weixin'
        // const title = <p className='download-title'>
        //     {app_name}App<br/>有你感兴趣的内容
        // </p>
        const title = <p className='download-title'>
            {app_name}App<br/>有你感兴趣的内容
        </p>
        const openBrowser = <div className='download-open-browser'>

            <img src={SquareIcon} alt='' />
            <p>请点击右上角<br/>选择“浏览器中打开”</p>
        </div>
        const logo = <div>
            {/*<img alt='' src={LogoIcon} className='download-logo'/>*/}
            <img alt='' src={logoUrl} className='download-logo'/>
            {/*<p className='download-app-name'>*/}
            {/*    {app_name}*/}
            {/*</p>*/}
            <p className='download-app-name'>
                {app_name}
            </p>
            <p className='download-app-url'>
                www.youtoball.com
            </p>
        </div>

        const cantTxt = <p className='download-cant'>微信/QQ内无法下载应用</p>
        const btn = <CustomButton
            onClick={this.toDownload}
        >
            下载
        </CustomButton>

        return <div
            className={wx ? 'download-ctn-wx' : 'download-ctn'}
        >
            {wx ? openBrowser : title}
            {logo}
            {wx ? cantTxt : btn}
        </div>
    }
}

export default Download
